package com.guiji;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guiji.file.domain.Resource;
import com.guiji.file.mapper.ResourceMapper;
import com.guiji.job.domain.Job;
import com.guiji.job.mapper.JobMapper;
import com.guiji.quartz.vo.LabelDO;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ActiveProfiles;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @Copyright (C), 2018-2021
 * @FileName: InsertJobAndResource
 * @Author: scl
 * @Date: 2021/10/29 11:49
 * @Description: 插入json数据
 */
@SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("dev")
public class InsertJobAndResource {


	@Autowired
	private ResourceMapper resourceMapper;

	@Autowired
	private JobMapper jobMapper;


	/**
	 * @descp: 思路：同时读取json文件+mp4路径
	 * @param: 同时插入resource表和job表,
	 * 把用户鉴权关闭,否则无法执行save（）；CreateAndUpdateMetaObjectHandler.insertFill
	 * this.setFieldValByName("createBy", SecurityUtils.getUsername(), metaObject);
	 * 第一步：将mp4文件拷到nginx服务器目录下
	 * 第二步：计算三层目录
	 * 第三步：配置@ActiveProfiles("dev")
	 * 第四步：执行。结束。
	 * 共用两种方法
	 * 第一种：simpleJsonInsert（）只有一个json文件
	 * 第二种：insert（）多个json文件在同一个目录下
	 * @Author: scl
	 * @Date: 2021/10/29 16:42
	 */
	@Test
	public void simpleJsonInsert() {

		File file = new File("C:\\Users\\Administrator\\Downloads\\fengshen_new.json");
		String s = FileUtil.readString(file, Charset.defaultCharset());
		JSONArray array = JSONUtil.parseArray(s);
		ArrayList<LabelDO> list = new ArrayList<LabelDO>();
		for (Object o : array) {
			JSONObject jsonObject = JSONUtil.parseObj(o);
			LabelDO lable = new LabelDO();
			lable.setStaticUrl(jsonObject.getStr("staticUrl"));
			lable.setDescription(jsonObject.getStr("description"));
			lable.setTags_person(jsonObject.getStr("tags_person"));
			lable.setDuration(Double.parseDouble(jsonObject.getStr("duration")));
			String num=jsonObject.getStr("staticUrl").replace("/text2video/videos/fengshen-shang/mp4/add-","").replace(".mp4","");
			//24-79，24-9，24-179
			if(num.length()==4){
				num=num.replace("-","00");
			}else if(num.length()==5){
				num=num.replace("-","0");
			}if(num.length()==6){
				num=num.replace("-","");
			}

			lable.setSortOrder(Integer.parseInt(num));
			list.add(lable);
		}

		Collections.sort(list, new Comparator<LabelDO>() {
			@Override
			public int compare(LabelDO o1, LabelDO o2) {
				return o1.getSortOrder()-o2.getSortOrder();
			}
		});

		for(LabelDO lable : list){

			String staticUrl = lable.getStaticUrl();
			staticUrl = staticUrl.replace("/text2video/videos/", "").replace(".ts", ".mp4");
			//插入resource表
			Resource add = new Resource();
			add.setUploadPath("http://172.16.6.72/" + staticUrl);//MP4视频nginx路径
			add.setMd5("");
			add.setName(staticUrl.replace("/", "_"));
			add.setPostfix("mp4");
			resourceMapper.insert(add);
			//System.out.println("resourceMapper.insert(add):"+add.getId());
			// 插入一个job
			Job job = new Job();
			job.setResourceId(Long.valueOf(add.getId()));
			job.setTemplateId(103L);//水浒打标模板
			job.setStatus(1);
			job.setPerson("");
			job.setBackup1(JSONUtil.parseObj(lable).toString());
			job.setCreateBy("admin");
			job.setCreateTime(new Date());
			jobMapper.insert(job);
			//System.out.println("for():" + lable.getStaticUrl());
		}

	}

	//@Test
	public void insert() {
		for (File file : FileUtil.ls("C:\\Users\\Administrator\\Downloads\\")) {
			String s = FileUtil.readString(file, Charset.defaultCharset());
			JSONArray array = JSONUtil.parseArray(s);
			//Console.log(array);
			for (Object o : array.stream().toArray()) {
				JSONObject jsonObject = JSONUtil.parseObj(o);
				String staticUrl = jsonObject.getStr("staticUrl");


				String filename = "xiyou0722_" + ReUtil.findAll("text2video/videos/xiyou_0722/(.*?)/\\d*.ts", staticUrl, 1).get(0) + "_" + staticUrl.substring(staticUrl.lastIndexOf("/") + 1, staticUrl.lastIndexOf(".ts")) + ".mp4";
				QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("file_name", filename);
				Resource resource = resourceMapper.selectOne(queryWrapper);

				// 插入一个job
				Job job = new Job();
				job.setResourceId(resource.getId());
				job.setTemplateId(103L);
				job.setStatus(1);
				job.setPerson("");
				job.setBackup1(jsonObject.toString());
				job.setCreateBy("admin");
				job.setCreateTime(new Date());
				//Console.log(job);
				jobMapper.insert(job);
			}
		}
	}
}
